package sem;

import com.ibm.tspaces.Tuple;
import com.ibm.tspaces.TupleSpace;
import com.ibm.tspaces.TupleSpaceException;

public class Semaphore {
  public  String     name;
  private TupleSpace space;
  private Tuple      tuple;

  public Semaphore(String name, TupleSpace space) 
  throws TupleSpaceException {
    this.space = space;
    this.name  = name;
    this.tuple = new Tuple(name);
  }

  public void init(int n) 
  throws TupleSpaceException {
    for (int i = 0; i < n; i++)
      space.write(tuple);
  }

  public void P() 
  throws TupleSpaceException {
    space.waitToTake(tuple);
  }

  public void V() 
  throws TupleSpaceException {
    space.write(tuple);
  }
}
